﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.Actions.Parameters;

namespace RibbonEditor.Controls.Actions.Parameters
{
    public partial class BoolParameterControl : UserControl, IControlRulable
    {
        private BoolParameter parameter;

        public BoolParameter Parameter
        {
            get { return this.parameter; }
            set { this.parameter = value; }
        }

        public BoolParameterControl(bool childOfUrl)
        {
            InitializeComponent();

            this.parameter = new BoolParameter(childOfUrl);
        }

        public BoolParameterControl(BoolParameter parameter)
            : this(parameter.IsChildOfUrl)
        {
            this.parameter = parameter;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.strName.Value = this.parameter.Name;
                this.bcValue.Value = this.parameter.Value;

                this.strName.Enabled = this.parameter.IsChildOfUrl;
                this.strName.IsRequired = this.parameter.IsChildOfUrl;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.parameter.Name = this.strName.Value;
                    this.parameter.Value = this.bcValue.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BoolParameterControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.parameter.AddParameterNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
